
//
// Copyright 2009 Arne Halvorsen (arne.halvorsen@gmail.com)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

// Project page: http://code.google.com/p/mf4j/

//
// 091116 - aha42 - Starts in line change tracking.
// 091119a- aha42 - Renamed method setType to matchType and aetAttribute to
//                  matchAttribute.
// 091119b- aha42 - LookupFlickrObjectsVisitor(String).
// 091119c- aha42 - search(FlickrObject) and search(FlickrObject[]). 
// 091119d- aha42 - Renamed from LookupFlickrObjectsVisitor.
// 091121 - aha42 - Added project page link (google code) to header comment.
// 091128 - aha42 - Missing @Overide.
//

package org.aha.mf4j.visitor;

import java.util.ArrayList;

import org.aha.mf4j.response.FlickrObject;

/**
 * <p>
 *   {@link Visitor} that finds a
 *   {@link FlickrObject} in hierarchy matching given
 *   {@link FlickrObject#getType()} and
 *   {@link FlickrObject#getAttribute(String) attributes}.
 * </p>
 * @author Arne Halvorsen (aha42)
 */
public class LookupVisitor extends Visitor 
{
  private String m_type=null;
  
  private final ArrayList<String> m_attrName=new ArrayList<String>();
  
  private final ArrayList<String> m_attrValues=new ArrayList<String>();
  
  private FlickrObject m_found=null;
  
  /**
   * <p>
   *   Constructor.
   * </p>
   */
  public LookupVisitor(){}
  
  /**
   * <p>
   *   Constructor.
   * </p>
   * @param type Type to match.
   */
  public LookupVisitor(String type)
  {
    if (type==null)
    {
      throw new NullPointerException("type");
    }
    
    m_type=null;
  }
  
  /**
   * <p>
   *   Clears attributes.
   * </p>
   * @return {@code this};
   */
  public LookupVisitor clearAttrs()
  {
    m_attrName.clear();
    m_attrValues.clear();
    return this;
  }
  
  /**
   * <p>
   *   Sets 
   *   {@link FlickrObject#getType() type} to look for.
   * </p>
   * @param type Type, may be {@code null} if to clear.
   * @return {@code this}.
   */
  public LookupVisitor matchType(String type)
  {
    m_type=type;
    return this;
  }
  
  /**
   * <p>
   *   Adds attribute name / value pair to match.
   * </p>
   * @param name Attribute name.
   * @param val  Attribute value, may be {@code null} if only to look for 
   *             object that has attribute.
   * @return {@code this}.
   */
  public LookupVisitor matchAttribute(String name, String val)
  {
    if (name==null)
    {
      throw new NullPointerException("name");
    }
    
    m_attrName.add(name);
    m_attrValues.add(val);
    return this;
  }
  
  /**
   * <p>
   *   Gets found object.
   * </p>
   * @return Found or {@code null} if none found or no search has been 
   *         performed yet.
   */
  public FlickrObject getFound(){ return m_found; }
  
  @Override
  public void start(){ m_found=null; }

  @Override
  public boolean visit(int level, FlickrObject visited) 
  {
    if (m_type!=null && !m_type.equals(visited.getType())) return true;
    
    int n=m_attrName.size();
    for (int i=0; i<n; i++)
    {
      String name=m_attrName.get(i);
      
      String visitedVal=visited.getAttribute(name);
      if (visitedVal==null) return true;
      
      String val=m_attrValues.get(i);
      if (val!=null && !val.equals(visitedVal)) return true;
    }
    
    m_found=visited;
    return false;
  }
  
  /**
   * <p>
   *   Search given hierarchy.
   * </p>
   * @param root Root of hierarchy.
   * @return Found or {@code null} if none found.
   */
  public FlickrObject search(FlickrObject root)
  {
    if (root==null)
    {
      throw new NullPointerException("root");
    }
    
    root.visit(this);
    return m_found;
  }
  
  /**
   * <p>
   *   Search given hierarchies.
   * </p>
   * @param forest Roots of hierarchy.
   * @return Found or {@code null} if none found.
   */
  public FlickrObject search(FlickrObject[] forest)
  {
    if (forest==null)
    {
      throw new NullPointerException("forest");
    }
    
    FlickrObject.visit(forest, this);
    return m_found;
  }
  
}
